package com.api.gbhg.huangwm.action;

import com.api.gbhg.huangwm.dao.RevenueBudgetDao;
import com.api.gbhg.liuwenbo.action.AmountVerification;
import com.api.gbhg.liwenjun.VersionFlowUtil;
import com.customization.commons.LogTool;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author	Huangwm
 * @Date 2021-12-01
 * @Version 1.0
 * @description	TODO 预算编制流程节点附件操作释放收入预算
 */
public class RevenueBudgetReturn implements Action{
    private static RevenueBudgetDao budgetUtils = new RevenueBudgetDao();
    private LogTool logTool = new LogTool("/log/gbhg/huangwm/action/RevenueBudgetReturn",false);

    private static String YJ ;//科目级别
    private static String XJ ;//科目下级

    @Override
    public String execute(RequestInfo requestInfo) {
        logTool.writeLog("RevenueBudgetReturn预算编制释放收入预算Action workflowid="+requestInfo.getWorkflowid()+"  requestid="+requestInfo.getRequestid()+" tablename="+requestInfo.getRequestManager().getBillTableName());
        BaseBean baseBean = new BaseBean();
        YJ = baseBean.getPropValue("Verification","yj");
        XJ = baseBean.getPropValue("Verification","xj");
        logTool.writeLog("财政拨款核批层级 YJ="+YJ+"  XJ="+XJ);
        int mainTableId = VersionFlowUtil.getFlowTableLableId(requestInfo.getWorkflowid()+"");//获取formid 例如：-83
        String mainTabName = AmountVerification.getMainTableName(mainTableId);//获取主表名
        Integer dw = AmountVerification.getwofkflowDw(mainTabName, requestInfo.getRequestid()+"");
        List<String> flowDetailTableNames = AmountVerification.getDetailTables(mainTableId);//获取明细表名称
        String flowTableName = VersionFlowUtil.getFlowTableName(requestInfo.getWorkflowid()+"");//获取表名
        String mainid = AmountVerification.getIdByTable(flowTableName, requestInfo.getRequestid()+""); //获取流程当前id

        String src = requestInfo.getRequestManager().getSrc();
        logTool.writeLog("此Action需在退回使用 当前src="+src);
        if(!src.equals("reject")){
            return SUCCESS;
        }

        List<Map<String,String>> getSNDatas = new ArrayList<>();//上年结转
        List<Map<String,String>> getDNDatas = new ArrayList<>();//当年预算
        String ysnd = "";
        try {
            ysnd = budgetUtils.getFieldValue("ysnd", flowTableName, requestInfo.getRequestid()+"");
            if(ysnd.isEmpty()){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("获取预算年度为空");
                return  FAILURE_AND_CONTINUE;
            }
        }catch (Exception e){
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("获取预算年度异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }
        try {
            getSNDatas = budgetUtils.getSNDatas(flowDetailTableNames.get(1),mainid);//上年结转
            getDNDatas = budgetUtils.getDNDatas(flowDetailTableNames.get(0),mainid);//当年预算
            logTool.writeLog(" 上年结转 "+getSNDatas);
            logTool.writeLog(" 当年预算 "+getDNDatas);
        }catch (Exception e){
            e.printStackTrace();
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("获取上年结转和当年预算核批数据异常 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }

        /**
         * 开始执行收入预算扣减逻辑
         */
        //先释放上年结转
        List<List<Object>> SNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> SNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateSNKM = "update "+budgetUtils.czbk_km+" set snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+? where id =?";
        String updateSNKMXJ = "update "+budgetUtils.czbk_xj+" set snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-?,yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+? where id =?";

        List<List<Object>> SNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> SNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateSNFZYSR = "update "+budgetUtils.fzysr+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+?,snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-? where id=?";
        String updateSNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yqndyjjzyyje=isnull(yqndyjjzyyje,0)-?,yqndyjjzsyje=yqndyjjzsyje+?,snjzhpje=isnull(snjzhpje,0)-?,snjzhpsyje=isnull(snjzhpsyje,0)-? where id=?";

        if(getSNDatas.size()>0){
            logTool.writeLog("--------上年结转--------");
            RecordSetTrans operation_SN = new RecordSetTrans();
            operation_SN.setAutoCommit(false);
            for(int i=0;i<getSNDatas.size();i++){
                Map<String,String> data = getSNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmsnjz = data.get("dfczbkxmsnjz");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        //System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            SNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        SNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmsnjz);
                        SNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmsnjz).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        SNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("--------上年结转--------");
        }

        //再释放当年预算
        List<List<Object>> DNparams_czbkkm = new ArrayList<>();//财政拨款科目参数
        List<List<Object>> DNparams_czbkkmxj = new ArrayList<>();//财政拨款科目下级参数
        String updateDNKM = "update "+budgetUtils.czbk_km+" set yhpje=isnull(yhpje,0)-?,hpsyje=ISNULL(hpsyje,0)-?,yjsrsy=yjsrsy+? where id =?";
        String updateDNKMXJ = "update "+budgetUtils.czbk_xj+" set yhpje=isnull(yhpje,0)-?,yhpsy=ISNULL(yhpsy,0)-?,yjsrsy=yjsrsy+? where id =?";

        List<List<Object>> DNparams_fczbk = new ArrayList<>();//非财政拨款参数
        List<List<Object>> DNparams_fczbkxm = new ArrayList<>();//非财政拨款项目参数
        String updateDNFZYSR = "update "+budgetUtils.fzysr+" set yhpje=isnull(yhpje,0)-?,hpsyje=isnull(hpsyje,0)-?,yjsrsy=yjsrsy+? where id=?";
        String updateDNFZYSRXM = "update "+budgetUtils.fzysrxm+" set yhpje=isnull(yhpje,0)-?,hpsyje=isnull(hpsyje,0)-?,yjsrsy=yjsrsy+? where id=?";

        if(getDNDatas.size()>0){
            logTool.writeLog("--------当年预算--------");
            for(int i=0;i<getDNDatas.size();i++){
                Map<String,String> data = getDNDatas.get(i);
                String id = data.get("id");
                String srlx = data.get("srlx");
                String zyczbksr = data.get("zyczbksr");
                String zcqd = data.get("zcqd");
                String dfczbkxmdnys = data.get("dfczbkxmdnys");
                String hpje = data.get("hpje");

                List<Object> czbkKMParams = new ArrayList<>();//财政拨款科目级
                List<Object> fczbkXMParams = new ArrayList<>();//非财政拨款项目级
                List<Object> fczbkFLParams = new ArrayList<>();//非财政拨款分类级
                //先判断srlx 0-财政拨款收入 1-非财政拨款收入
                if(srlx.equals("0")){
                    //System.out.println("本条数据为财政拨款");
                    //zyczbksr 的格式为 26_87和25_36 25为一级 26为下级
                    String yjORxj = zyczbksr.split("_")[0];
                    //当yjORxj为YJ的时候 dataid为科目表id 否则为科目下级表id
                    String dataid = zyczbksr.split("_")[1];
                    if(YJ.equals(yjORxj)){
                        //System.out.println("核批为科目级："+dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(dataid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }else{
                        //根据dataid获取所有上级id 含本身 用于扣减收入
                        List<String> superiorId = budgetUtils.getSuperiorId(dataid);
                        // System.out.println("核批为科目下级："+dataid + ">>" +superiorId.toString());
                        //由于包含上级时可能存在多个 需循环
                        for (int j=0;j<superiorId.size();j++){
                            List<Object> czbkXJParams = new ArrayList<>();//财政拨款科目下级
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(hpje);
                            czbkXJParams.add(superiorId.get(j));
                            DNparams_czbkkmxj.add(czbkXJParams);
                        }

                        //科目下级还有对应的科目级需要扣  获取对应的科目级id
                        String kmid = budgetUtils.getZykmidByxjid(dataid);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(hpje);
                        czbkKMParams.add(kmid);
                        DNparams_czbkkm.add(czbkKMParams);
                    }
                }else{
                    //System.out.println("本条数据为非财政拨款");
                    //先判断收入分类 5和8为其他收入和地方拨款项目支出 需要根据具体的项目id扣减
                    if(zcqd.equals("5") || zcqd.equals("8")){
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(hpje);
                        fczbkXMParams.add(dfczbkxmdnys);
                        DNparams_fczbkxm.add(fczbkXMParams);
                        //需要再扣减收入项目所在收入分类
                        String szflid = budgetUtils.getFzysrxmFL(dfczbkxmdnys).get("szfl");
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }else{
                        //非其他收入和地方拨款项目支出时  直接扣减分类 根据单位、预算年度和分类找表id
                        String szflid = budgetUtils.getFzysrid(dw+"",ysnd,zcqd);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(hpje);
                        fczbkFLParams.add(szflid);
                        DNparams_fczbk.add(fczbkFLParams);
                    }
                }
            }
            logTool.writeLog("--------当年预算--------");
        }

        RecordSetTrans operation = new RecordSetTrans();
        operation.setAutoCommit(false);
        boolean f1 = true,f2 = true,f3 = true,f4 = true;
        boolean f5 = true,f6 = true,f7 = true,f8 = true;
        //释放当年财政拨款科目金额
        try {
            operation.executeBatchSql(updateDNKM, DNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f1 = false;
            logTool.writeLog("释放当年财政拨款科目金额SQL执行异常");
        }
        //释放当年财政拨款科目下级金额
        try {
            operation.executeBatchSql(updateDNKMXJ, DNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f2 = false;
            logTool.writeLog("释放当年财政拨款科目下级金额SQL执行异常");
        }
        //释放当年非中央收入分类金额
        try {
            operation.executeBatchSql(updateDNFZYSR, DNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f3 = false;
            logTool.writeLog("释放当年非中央收入分类金额SQL执行异常");
        }
        //释放当年非中央项目收入金额
        try {
            operation.executeBatchSql(updateDNFZYSRXM, DNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f4 = false;
            logTool.writeLog("释放当年非中央项目收入金额SQL执行异常");
        }

        //释放上年结转财政拨款科目收入金额
        try {
            operation.executeBatchSql(updateSNKM, SNparams_czbkkm);
        }catch (Exception e){
            e.printStackTrace();
            f5 = false;
            logTool.writeLog("释放上年结转财政拨款科目收入金额SQL执行异常");
        }
        //释放上年结转财政拨款科目下级收入金额
        try {
            operation.executeBatchSql(updateSNKMXJ, SNparams_czbkkmxj);
        }catch (Exception e){
            e.printStackTrace();
            f6 = false;
            logTool.writeLog("释放上年结转财政拨款科目下级收入金额SQL执行异常");
        }
        //释放上年结转非财政拨款收入分类金额
        try {
            operation.executeBatchSql(updateSNFZYSR, SNparams_fczbk);
        }catch (Exception e){
            e.printStackTrace();
            f7 = false;
            logTool.writeLog("释放上年结转非财政拨款收入分类金额SQL执行异常");
        }
        //释放上年结转非财政拨款收入项目金额
        try {
            operation.executeBatchSql(updateSNFZYSRXM, SNparams_fczbkxm);
        }catch (Exception e){
            e.printStackTrace();
            f8 = false;
            logTool.writeLog("释放上年结转非财政拨款收入项目金额SQL执行异常");
        }

        if(f1 && f2 && f3 && f4 && f5 && f6 && f7 && f8){
            operation.commit();
            logTool.writeLog("SUCCESS 收入预算金额释放成功");
        }else{
            operation.rollback();
            if(!f1){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放当年财政拨款科目金额SQL执行异常 ");
                return  FAILURE_AND_CONTINUE;
            }
            if(!f2){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放当年财政拨款科目下级金额SQL执行异常");
                return  FAILURE_AND_CONTINUE;
            }
            if(!f3){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放当年非中央收入本级金额SQL执行异常");
                return  FAILURE_AND_CONTINUE;
            }
            if(!f4){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放当年非中央项目收入金额SQL执行异常");
                return  FAILURE_AND_CONTINUE;
            }
            if(!f5){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放上年结转非中央项目收入金额SQL执行异常");
                return  FAILURE_AND_CONTINUE;
            }
            if(!f6){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放上年结转非中央项目收入金额SQL执行异常");
                return  FAILURE_AND_CONTINUE;
            }
            if(!f7){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放上年结转非财政拨款收入分类金额SQL执行异常");
                return  FAILURE_AND_CONTINUE;
            }
            if(!f8){
                requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
                requestInfo.getRequestManager().setMessagecontent("释放上年结转非财政拨款收入项目金额SQL执行异常");
                return  FAILURE_AND_CONTINUE;
            }
        }

        return  SUCCESS;
    }
}
